package com.yishusmt.wechatapi.wechatdev.utils;

import com.yishusmt.wechatapi.wechatdev.dto.base.tuling.BaseEmotion;
import com.yishusmt.wechatapi.wechatdev.dto.base.tuling.BaseResults;
import com.yishusmt.wechatapi.wechatdev.dto.request.RobotResultReq;
import com.yishusmt.wechatapi.wechatdev.entity.*;
import com.yishusmt.wechatapi.wechatdev.entity.tulingentity.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

/**
 * 客服功能
 *
 * @author zhangyi
 * @date 2018/8/29 16:04
 */
@Component
public class CustomerUtil {

    @Autowired
    private HttpClientUtil httpClientUtil;

    /**
     * 智能机器人请求接口
     */
    public static final String ROBOT_URL = "http://openapi.tuling123.com/openapi/api/v2";

    /**
     * 智能机器人简单回复接口（GET请求）
     */
    public static final String ROBOT_SIMPLE_URL = "http://www.tuling123.com/openapi/api?key=KEY&info=";

    /**
     * 云端机器人唯一标识
     */
    public static final String ROBOT_API_KEY = "2bc9822e313f41ef871fa89e3619108d";

    /**
     * 本地用户为唯一标识
     */
    public static final String ROBOT_USER_ID = "1";

    /**
     * 自动客服回复
     *
     * @param sendStr
     * @return
     */
    public String robot(String sendStr) throws IOException, URISyntaxException {
        //json数据映射实体
        RobotEntity robotEntity = new RobotEntity();
        PerceptionEntity perceptionEntity = new PerceptionEntity();
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        SelfInfoEntity selfInfoEntity = new SelfInfoEntity();
        InputTextEntity inputTextEntity = new InputTextEntity();
        InputImageEntity inputImageEntity = new InputImageEntity();
        LocationEntity locationEntity = new LocationEntity();
        BaseEmotion baseEmotion = new BaseEmotion();

        userInfoEntity.setApiKey(ROBOT_API_KEY);
        userInfoEntity.setUserId(ROBOT_USER_ID);

        locationEntity.setCity("杭州");
        locationEntity.setProvince("浙江");
        locationEntity.setStreet("梦想小镇");

        inputTextEntity.setText(sendStr);

        selfInfoEntity.setLocation(locationEntity);
        perceptionEntity.setInputText(inputTextEntity);
        perceptionEntity.setInputImage(inputImageEntity);
        perceptionEntity.setSelfInfoEntity(selfInfoEntity);

        robotEntity.setPerception(perceptionEntity);
        robotEntity.setUserInfo(userInfoEntity);
        robotEntity.setEmotion(baseEmotion);

        String sendJson = JacksonUtil.toJSon(robotEntity);
        //接受消息实体
        String n = new String(httpClientUtil.postConnect(ROBOT_URL, sendJson).getBytes("iso-8859-1"),"utf8");
        System.out.println(n);
        if(StringUtils.contains(sendStr,MessageHandlerUtil.NEW)){
            return n;
        }else{
            //将json数据映射成对象
            RobotResultReq robotResultReq = JacksonUtil.readValue(n, RobotResultReq.class);
            List<BaseResults> results = robotResultReq.getResults();
            StringBuffer stringBuffer = new StringBuffer();
            results.stream().forEach(e -> stringBuffer.append(e.getValues().getText()));
            return stringBuffer.toString();
        }
    }
}
